﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ControllerManager : MonoBehaviour {

    #region 单例模式
    private static ControllerManager _instance;

    public static ControllerManager Instance {
        get {
            if (_instance == null) {
                _instance = GameObject.Find("ControllerManager").GetComponent<ControllerManager>();
            }
            return _instance;
        }
    }
    #endregion

    // Use this for initialization
    void Start () {
        GameObject.DontDestroyOnLoad(this);
    }
	
	// Update is called once per frame
	void Update () {
        if (Input.GetKeyDown(KeyCode.Escape)) { // Esc控制设置面板
            print("esc");
            bool isAction = false;
            GameObject knapsackObject = GameObject.FindGameObjectWithTag("Knapsack");
            if (knapsackObject != null) {
                Knapsack knapsack = knapsackObject.GetComponent<Knapsack>();
                if (knapsack.IsShowing()) { // 如果背包显示，则隐藏
                    knapsack.Hide();
                    isAction = true;
                }
            }
            GameObject propPanelObject = GameObject.FindGameObjectWithTag("PropPanel");
            if (propPanelObject != null) {
                PropPanel propPanel = propPanelObject.GetComponent<PropPanel>();
                if (propPanel.IsShowing()) { // 如果属性面板显示，则隐藏
                    propPanel.Hide();
                    isAction = true;
                }
            }
            print(ArticleManager.Instance);
            if (!isAction) { // 如果没有行为，再判断是否显示菜单
                GameObject settingObject = GameObject.FindGameObjectWithTag("Setting");
                if (settingObject != null) {
                    settingObject.GetComponent<SettingPanel>().Toggle();
                }
            }
        }
        if (Input.GetKeyDown(KeyCode.C) && ArticleManager.Instance != null) { // C控制人物面板
            print("c");
            ArticleManager.Instance.TogglePropPanel();
        }
        if (Input.GetKeyDown(KeyCode.K) && ArticleManager.Instance != null) { // K控制背包
            print("k");
            ArticleManager.Instance.ToggleKnapsack();
        }
        if (Input.GetKeyDown(KeyCode.F) && ArticleManager.Instance != null) { // F控制武学面板
            print("f");
            ArticleManager.Instance.ToggleWuxuePanel();
        }
        if (Input.GetKeyDown(KeyCode.Space) && ArticleManager.Instance != null) { // Space控制新增装备
            print("space");
            ArticleManager.Instance.StoreArticle();
        }
        if (Input.GetKeyDown(KeyCode.P)) { // P控制速度
            print("p");
            GlobalData globalData = GlobalData.Instance;
            if (globalData.GameSpeed == 1) {
                Time.timeScale = globalData.GameSpeed = 2;
            } else {
                Time.timeScale = globalData.GameSpeed = 1;
            }
        }
    }

    public void NewGame() {
        GameManager.Instance.NewGame();
    }

    public void SaveGame() {
        GameManager.Instance.SaveGame();
        SettingPanel SettingPanel = GameObject.Find("SettingPanel").GetComponent<SettingPanel>();
        SettingPanel.Hide();
    }

    public void LoadGame() {
        GameManager.Instance.LoadGame();
    }

    public void BackToMenu() {
        GameManager.Instance.BackToMenu();
    }

    public void ExitGame() {
        GameManager.Instance.ExitGame();
    }
}
